home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 / Programming Windows 95.iso / code / CHAP07 / BEEPER2.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-01  |  2.8 KB  |  90 lines

  1. /*----------------------------------------
  2.    BEEPER2.C -- Timer Demo Program No. 2
  3.                 (c) Charles Petzold, 1996
  4.   ----------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. #define ID_TIMER    1
  9.  
  10. LRESULT CALLBACK WndProc   (HWND, UINT, WPARAM, LPARAM) ;
  11. VOID    CALLBACK TimerProc (HWND, UINT, UINT,   DWORD ) ;
  12.  
  13. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  14.                     PSTR szCmdLine, int iCmdShow)
  15.      {
  16.      static char szAppName[] = "Beeper2" ;
  17.      HWND        hwnd ;
  18.      MSG         msg ;
  19.      WNDCLASSEX  wndclass ;
  20.  
  21.      wndclass.cbSize        = sizeof (wndclass) ;
  22.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  23.      wndclass.lpfnWndProc   = WndProc ;
  24.      wndclass.cbClsExtra    = 0 ;
  25.      wndclass.cbWndExtra    = 0 ;
  26.      wndclass.hInstance     = hInstance ;
  27.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  28.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  29.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  30.      wndclass.lpszMenuName  = NULL ;
  31.      wndclass.lpszClassName = szAppName ;
  32.      wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;
  33.  
  34.      RegisterClassEx (&wndclass) ;
  35.  
  36.      hwnd = CreateWindow (szAppName, "Beeper2 Timer Demo",
  37.                           WS_OVERLAPPEDWINDOW,
  38.                           CW_USEDEFAULT, CW_USEDEFAULT,
  39.                           CW_USEDEFAULT, CW_USEDEFAULT,
  40.                           NULL, NULL, hInstance, NULL) ;
  41.  
  42.      while (!SetTimer (hwnd, ID_TIMER, 1000, (TIMERPROC) TimerProc))
  43.           if (IDCANCEL == MessageBox (hwnd,
  44.                               "Too many clocks or timers!", szAppName,
  45.                               MB_ICONEXCLAMATION | MB_RETRYCANCEL))
  46.                return FALSE ;
  47.  
  48.      ShowWindow (hwnd, iCmdShow) ;
  49.      UpdateWindow (hwnd) ;
  50.  
  51.      while (GetMessage (&msg, NULL, 0, 0))
  52.           {
  53.           TranslateMessage (&msg) ;
  54.           DispatchMessage (&msg) ;
  55.           }
  56.      return msg.wParam ;
  57.      }
  58.  
  59. LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  60.      {
  61.      switch (iMsg)
  62.           {
  63.           case WM_DESTROY :
  64.                KillTimer (hwnd, ID_TIMER) ;
  65.                PostQuitMessage (0) ;
  66.                return 0 ;
  67.           }
  68.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  69.      }
  70.  
  71. VOID CALLBACK TimerProc (HWND hwnd, UINT iMsg, UINT iTimerID, DWORD dwTime)
  72.      {
  73.      static BOOL fFlipFlop = FALSE ;
  74.      HBRUSH      hBrush ;
  75.      HDC         hdc ;
  76.      RECT        rc ;
  77.  
  78.      MessageBeep (0) ;
  79.      fFlipFlop = !fFlipFlop ;
  80.  
  81.      GetClientRect (hwnd, &rc) ;
  82.  
  83.      hdc = GetDC (hwnd) ;
  84.      hBrush = CreateSolidBrush (fFlipFlop ? RGB(255,0,0) : RGB(0,0,255)) ;
  85.  
  86.      FillRect (hdc, &rc, hBrush) ;
  87.      ReleaseDC (hwnd, hdc) ;
  88.      DeleteObject (hBrush) ;
  89.      }
  90.